const db = require('../common/db');
const sql = require('../common/sql');

module.exports = {
    catebyid: (req, res) => {
        let cgid = req.body.cgid;
        db.query(sql.goods.catebyid, [cgid], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: '查询数据库错误。' });
            } else {
                res.send({ code: 200, category: results[0] });
            }
        });
    },
    goodslist: (req, res) => {
        let cgid = req.body.cgid;
        db.query(sql.goods.goodslist, [cgid], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: '查询数据库错误。' });
            } else {
                res.send({ code: 200, goods: results });
            }
        });
    },
    goodsByid: (req, res) => {
        let gid = req.body.gid;
        db.query(sql.goods.goodsByid, [gid, gid], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, goods: results[0] })
            }
        });
    },
    all: (req, res) => {
        let skey = req.body.skey;
        if (!skey) {
            skey = '';
        }
        db.query(sql.goods.all, ['%' + skey + '%'], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, goods: results });
            }
        });
    },
    lunbo: (req, res) => {
        let gid = req.body.gid;
        db.query(sql.goods.lunbo, ['%' + gid + '%'], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, goods: results });
            }
        });
    },
    xiaomi: (req, res) => {
        let gid = req.body.gid;
        db.query(sql.goods.xiaomi, ['%' + gid + '%'], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
                } else {
                    res.send({ code: 200, goods: results });
                }
        });
    },
    redmi: (req, res) => {
        let gid = req.body.gid;
        db.query(sql.goods.redmi, ['%' + gid + '%'], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, goods: results });
            }
        });
    },
    book: (req, res) => { 
        let gid = req.body.gid;
        db.query(sql.goods.book, ['%' + gid + '%'], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, goods: results });
            }
        });
    },
    pad: (req, res) => { 
        let gid = req.body.gid;
        db.query(sql.goods.pad, ['%' + gid + '%'], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, goods: results });
            }
        });
    },
    dianshi: (req, res) => { 
        let gid = req.body.gid;
        db.query(sql.goods.dianshi, ['%' + gid + '%'], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, goods: results });
            }
        });
    },
    jiadian: (req, res) => { 
        let gid = req.body.gid;
        db.query(sql.goods.jiadian, ['%' + gid + '%'], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, goods: results });
            }
        });
    },
    luyou: (req, res) => { 
        let gid = req.body.gid;
        db.query(sql.goods.luyou, ['%' + gid + '%'], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, goods: results });
            }
        });
    },
     updatelunbo: (req, res) => {
        const { gname, img, price, introduce, gid } = req.body;
        db.query(sql.goods.updatelunbo, [gname, img, price, introduce, gid], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, goods: results });
            }
        })
    },
    goodsinfo: (req, res) => {
        let gid = req.body.gid;
        db.query(sql.goods.goodsinfo, [gid], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, goods: results });
            }
        });
    },
    updateGoods: (req, res) => {
        let { gid, gname, img, cgid, introduce, addtime, price } = req.body;

        // 验证数据类型
        if (isNaN(Number(cgid)) || isNaN(Number(price))) {
            return res.send({ code: 400, msg: "分类ID和价格必须是数字" });
        }

        // 转换数据类型
        cgid = Number(cgid);
        price = Number(price);

        db.query(sql.goods.updateGoods,
            [gname, img, cgid, price, introduce, addtime, gid],
            (err, results) => {
                if (err) {
                    console.error("数据库错误:", err);
                    res.send({ code: 500, msg: err.message });
                } else {
                    res.send({
                        code: 200,
                        affectedRows: results.affectedRows
                    });
                }
            });
    },
    addGoods: (req, res) => {
        let { gname, img, cgid, price, introduce, addtime, gid } = req.body;
        db.query(sql.goods.addGoods, [gname, img, cgid, price, introduce, addtime, gid], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, goods: results });
            }
        })
    },
    addlunbo: (req, res) => {
        let { gname, img, cgid,price, introduce,addtime, gid } = req.body;
        db.query(sql.goods.addlunbo, [gname, img, cgid,price, introduce,addtime, gid], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, goods: results });
            }
        })
    },
    delGoods: (req, res) => {
        let gid = req.body.gid;
        db.query(sql.goods.delGoods, [gid], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, goods: results });
            }
        })
    },
    delLunbo: (req, res) => {
        let gid = req.body.gid;
        db.query(sql.goods.delLunbo, [gid], (err, results) => {
            if (err) {
                res.send({ code: 500, msg: err.message });
            } else {
                res.send({ code: 200, goods: results });
            }
        })
    }
}